HackerRank Common Child
https://www.hackerrank.com/challenges/common-child/problem
解答
code: python
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'commonChild' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
# 1. STRING s1
# 2. STRING s2
#
def commonChild(s1, s2):
l_s = len(s1)
dp = [0 * (l_s + 1) for i in range(l_s + 1)]
for i in range(l_s):
for j in range(l_s):
if (s1i == s2j):
dpi+1j+1 = dpij + 1
else:
dpi+1j+1 = max(dpij+1, dpi+1j)
return dpl_sl_s
if __name__ == '__main__':
fptr = open(os.environ'OUTPUT_PATH', 'w')
s1 = input()
s2 = input()
result = commonChild(s1, s2)
fptr.write(str(result) + '\n')
fptr.close()
テーマ
#dp
メモ
A20 - LCS
提出
code: python
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'commonChild' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
# 1. STRING s1
# 2. STRING s2
#
# HARRY
# SALLY
# - Define start
# -- H not in SALLY O(len(s))
# -- A in SALLY O(len(s))
# --- ...
# O(len(s)) * O(len(s))
def commonChild(s1, s2):
# Write your code here
if __name__ == '__main__':
fptr = open(os.environ'OUTPUT_PATH', 'w')
s1 = input()
s2 = input()
result = commonChild(s1, s2)
fptr.write(str(result) + '\n')
fptr.close()